package cn.iocoder.yudao.module.hs.controller.admin.entryorder;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.entryorder.vo.*;
import cn.iocoder.yudao.module.hs.controller.admin.trayscanrecord.vo.TrayScanRecordRespVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.entryorder.EntryOrderDO;
import cn.iocoder.yudao.module.hs.service.entryorder.EntryOrderService;
import cn.iocoder.yudao.module.hs.service.trayscanrecord.TrayScanRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 入库单")
@RestController
@RequestMapping("/hs/entry-order")
@Validated
public class EntryOrderController {

    @Resource
    private EntryOrderService entryOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建入库单")
    @PreAuthorize("@ss.hasPermission('hs:entry-order:create')")
    public CommonResult<Long> createEntryOrder(@Valid @RequestBody EntryOrderSaveReqVO createReqVO) {
        return success(entryOrderService.createEntryOrder(createReqVO));
    }

    @PostMapping("/tripartite-create")
    @Operation(summary = "创建三方入库单")
    @PreAuthorize("@ss.hasPermission('hs:entry-order:create-tripartite')")
    public CommonResult<Boolean> createTripartiteEntryOrder(@Valid @RequestBody TripartiteEntryOrderSaveReqVO createReqVO) {
        entryOrderService.createTripartiteEntryOrder(createReqVO);
        return success(Boolean.TRUE);
    }

    @PostMapping("/quick-create")
    @Operation(summary = "创建快速入库单")
    @PreAuthorize("@ss.hasPermission('hs:entry-order:create')")
    public CommonResult<Boolean> createQuickEntryOrder(@Valid @RequestBody QuickEntryOrderSaveReqVO createReqVO) {
        entryOrderService.createQuickEntryOrder(createReqVO);
        return success(Boolean.TRUE);
    }

    @PutMapping("/update")
    @Operation(summary = "更新入库单")
    @PreAuthorize("@ss.hasPermission('hs:entry-order:update')")
    public CommonResult<Boolean> updateEntryOrder(@Valid @RequestBody EntryOrderSaveReqVO updateReqVO) {
        entryOrderService.updateEntryOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除入库单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:entry-order:delete')")
    public CommonResult<Boolean> deleteEntryOrder(@RequestParam("id") Long id) {
        entryOrderService.deleteEntryOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得入库单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:entry-order:query')")
    public CommonResult<EntryOrderRespVO> getEntryOrder(@RequestParam("id") Long id) {
        EntryOrderDO entryOrder = entryOrderService.getEntryOrder(id);
        return success(BeanUtils.toBean(entryOrder, EntryOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得入库单分页")
    @PreAuthorize("@ss.hasPermission('hs:entry-order:query')")
    public CommonResult<PageResult<EntryOrderRespVO>> getEntryOrderPage(@Valid EntryOrderPageReqVO pageReqVO) {
        PageResult<EntryOrderRespVO> pageResult = entryOrderService.getEntryOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EntryOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出入库单 Excel")
    @PreAuthorize("@ss.hasPermission('hs:entry-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEntryOrderExcel(@Valid EntryOrderPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "入库单.xls", "数据", EntryOrderRespVO.class,
                BeanUtils.toBean(entryOrderService.getEntryOrderPage(pageReqVO).getList(), EntryOrderRespVO.class));
    }

    @GetMapping("/get-tray-list")
    @Operation(summary = "获得入库单中托盘列表")
    @Parameter(name = "shipOrderId", description = "收货单对应的出库单ID", required = true)
    @Parameter(name = "warehouseId", description = "仓库ID", required = true)
    @PreAuthorize("@ss.hasPermission('hs:entry-order:query')")
    public CommonResult<List<EntryOrderTrayRespVO>> getTrayList(@RequestParam("shipOrderId") Long shipOrderId, @RequestParam("warehouseId") Long warehouseId) {
        return success(entryOrderService.getTrayList(shipOrderId, warehouseId));
    }
}